*-----------------------------------------------------------------; * Computes basic statistics by a single classification variable ; * called BYVAR. If specified, data set setup is merged with ; * output from this macro. ; * Output data set OUTSTATS has the following variables: ; * BYVAR - classification variable ; * YBAR - mean of response by classification variable, BYVAR ; * VAR - variance of response variable by BYVAR ; * STD - standard deviation of response variable by BYVAR ; * NI - number of observations by BYVAR ; *-----------------------------------------------------------------; %macro stats(noprint,sample=,byvar=,response=,setup=,outstats=); %if %length(&outstats) = 0 %then %let outstats = %str(outstats); %if %length(&sample) = 0 %then %let sample = %str(sample); %if %length(&byvar) = 0 %then %let byvar = %str(byvar); proc sort data = &sample; by &byvar; proc means data = &sample noprint; by &byvar; var &response; output out = &outstats mean = ybar std = std var = var n = ni; %if %length(&setup) > 0 %then %do; proc sort data = &setup; by &byvar; data &outstats; merge &setup &outstats; by &byvar; %end; %else %do; data &outstats; set &outstats; %end; drop _type_ _freq_; %if %length(&noprint) = 0 %then %do; proc print data = &outstats noobs split='*'; label ybar = 'Mean*(YBAR)'; label std = 'Standard*Deviation*(STD)'; label var = 'Variance*(VAR)'; label ni = 'Sample Size*(NI)'; title1 "Statistics for &response by &byvar"; title2 "Input Data Set = &sample"; title3 "Output Data Set = &outstats"; %end; run; title; %mend stats;